package com.cn.goodsadmin.service;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.common.observes.MessageConfig;
import com.cn.goodsadmin.common.observes.MessageManager;
import com.cn.goodsadmin.observer.UpdateOrderStatusMessage;
import com.cn.goodsadmin.po.Logistics;
import com.cn.goodsadmin.po.UserRealDestDTO;
import com.cn.goodsadmin.po.Userreal;
import com.cn.goodsadmin.util.TimeUtil;
import org.springframework.stereotype.Service;

@Service("userrealService")
public class UserRealService extends BaseService{

    public PageQueryResult<UserRealDestDTO> findAllUserReal(int status, int pageIndex, int pageSize, int appid) throws Exception {
        return userRealDao.findAllUserReal(status,pageIndex,pageSize,appid);
    }

    public void update(Userreal userreal) throws Exception{
        userRealDao.update(userreal);
    }

    public void  sendChangeOrder(int realGoodsid,int appid,String logisticsid,String logisticsname)throws Exception{
        Userreal userreal=userRealDao.findUserrealById(realGoodsid);
        if(userreal!=null) {
            int oldstatuts = userreal.getcIsuse();
            if (oldstatuts == 0) {
                //检查订单号是否存在
                Logistics logistics=logisticsDao.findLogisticsByLid(logisticsid);
                if(logistics==null) {
                    //入库添加物流
                    logistics = new Logistics();
                    logistics.setcLogisticsid(logisticsid);
                    logistics.setcName(logisticsname);
                    logisticsDao.save(logistics);
                }else{
                    String name=logistics.getcName();
                    if(!logisticsname.equals(name)) {
                        //更新
                        logistics.setcName(logisticsname);
                        logisticsDao.update(logistics);
                    }
                }
                //修改订单
                userreal.setcIsuse(1);
                userreal.setcLogisticsid(logisticsid);
                userreal.setcMtime(TimeUtil.getCurrTimeHm());
                userRealDao.update(userreal);
                UpdateOrderStatusMessage message = new UpdateOrderStatusMessage(MessageConfig.UPDATEORDERSTATUTS, userreal);
                MessageManager.getInstanceMessage().putMessage(message);
            }
        }
    }
}
